package cn.kinggn520;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 作者: kinggm Email:731586355@qq.com
 * 时间:  2020-05-13 20:26
 */
@Component
public class MyListener implements ChannelAwareMessageListener{

//    监听延迟队列
    @RabbitListener(queues = "order_queue_dlx")
    public void myListener1(String message){
        System.out.println("延时队列的消息==========》》》》"+message);
    }


    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
//            如果消息处理成功 就调用channel的basicAck()签收

//       接收转换消息
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            System.out.println(new String(message.getBody()));

            System.out.println("处理业务逻辑");

            System.out.println("手动签收");

            channel.basicAck(deliveryTag,true);

        } catch (IOException e) {
//            e.printStackTrace();
//            如果消息处理失败 就调用channel的basicNack()拒绝签收
//            拒绝签收
            channel.basicNack(deliveryTag,true,true);

        }



    }


}
